home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gnugo1_1.lha / gnugo / genmove.c < prev    next >
C/C++ Source or Header  |  1989-03-07  |  3KB  |  145 lines

  1. /*
  2.                 GNU GO - the game of Go (Wei-Chi)
  3.                 Version 1.1   last revised 3-1-89
  4.            Copyright (C) Free Software Foundation, Inc.
  5.                       written by Man L. Li
  6.                       modified by Wayne Iba
  7.                     documented by Bob Webber
  8. */
  9. /*
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation - version 1.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License in file COPYING for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. Please report any bug/fix, modification, suggestion to
  24.  
  25. mail address:   Man L. Li
  26.                 Dept. of Computer Science
  27.                 University of Houston
  28.                 4800 Calhoun Road
  29.                 Houston, TX 77004
  30.  
  31. e-mail address: manli@cs.uh.edu         (Internet)
  32.                 coscgbn@uhvax1.bitnet   (BITNET)
  33.                 70070,404               (CompuServe)
  34. */
  35.  
  36. #include <stdio.h>
  37.  
  38. #define EMPTY 0
  39. #define MAXTRY 400
  40.  
  41. extern unsigned char p[19][19];
  42. extern int mymove, umove;
  43. extern int rd, lib, pass;
  44.  
  45. genmove(i, j)
  46. /* generate computer move */
  47. int *i, *j;
  48.   {
  49.    int ti, tj, tval;
  50.    char a;
  51.    int ii, m, n, val;
  52.    int try = 0;   /* number of try */
  53.  
  54. /* initialize move and value */
  55.    *i = -1;  *j = -1;  val = -1;
  56.  
  57. /* re-evaluate liberty of opponent pieces */
  58.    eval(umove);
  59.  
  60. /* find opponent piece to capture or attack */
  61.    if (findwinner(&ti, &tj, &tval))
  62.        if (tval > val)
  63.      {
  64.       val = tval;
  65.       *i = ti;
  66.       *j = tj;
  67.     }
  68.  
  69. /* save any piece if threaten */
  70.    if (findsaver(&ti, &tj, &tval))
  71.        if (tval > val)
  72.      {
  73.       val = tval;
  74.       *i = ti;
  75.       *j = tj;
  76.     }
  77.  
  78. /* try match local play pattern for new move */
  79.    if (findpatn(&ti, &tj, &tval))
  80.        if (tval > val)
  81.      {
  82.       val = tval;
  83.       *i = ti;
  84.       *j = tj;
  85.     }
  86.  
  87. /* no urgent move then do random move */
  88.    if (val < 0)
  89.        do {
  90.        random(&rd);
  91.        *i = rd % 19;
  92. /* avoid low line  and center region */
  93.        if ((*i < 2) || (*i > 16) || ((*i > 5) && (*i < 13)))
  94.          {
  95.           random(&rd);
  96.           *i = rd % 19;
  97.           if ((*i < 2) || (*i > 16))
  98.         {
  99.          random(&rd);
  100.          *i = rd % 19;
  101.            }
  102.         }
  103.        random(&rd);
  104.        *j = rd % 19;
  105. /* avoid low line and center region */
  106.        if ((*j < 2) || (*j > 16) || ((*j > 5) && (*j < 13)))
  107.          {
  108.           random(&rd);
  109.           *j = rd % 19;
  110.           if ((*j < 2) || (*j > 16))
  111.         {
  112.          random(&rd);
  113.          *j = rd % 19;
  114.            }
  115.         }
  116.         lib = 0;
  117.         countlib(*i, *j, mymove);
  118.        }
  119. /* avoid illegal move, liberty one or suicide, fill in own eye */
  120.        while ((++try < MAXTRY)
  121.           && ((p[*i][*j] != EMPTY) || (lib < 2) || fioe(*i, *j)));
  122.  
  123.    if (try >= MAXTRY)  /* computer pass */
  124.      {
  125.       pass++;
  126.       printf("I pass.\n");
  127.       *i = -1;
  128.     }
  129.    else   /* find valid move */
  130.      {
  131.       pass = 0;      
  132.       printf("my move: ");
  133.       if (*j < 8)
  134.      a = *j + 65;
  135.       else
  136.      a = *j + 66;
  137.       printf("%c", a);
  138.       ii = 19 - *i;
  139.       if (ii < 10)
  140.      printf("%1d\n", ii);
  141.       else
  142.      printf("%2d\n", ii);
  143.     }
  144. }  /* end genmove */
  145.